home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0035_LongInt to HEX.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  1KB  |  42 lines

  1. {
  2. GREG VIGNEAULT
  3.  
  4. > So to assign the File I will need the HEX in String format.
  5. }
  6.  
  7. Type
  8.   String8 = String[8];
  9.  
  10. Var
  11.   MyStr : String8;
  12.   ALong : LongInt;
  13.  
  14. { convert a LongInt value to an 8-Character String, using hex digits  }
  15. { (using all 8 Chars will allow correct order in a sorted directory)  }
  16.  
  17. Procedure LongToHex(AnyLong : LongInt; Var HexString : String8);
  18. Var
  19.   ch    : Char;
  20.   Index : Byte;
  21. begin
  22.   HexString := '00000000';                  { default to zero   }
  23.   Index := Length(HexString);               { String length     }
  24.   While AnyLong <> 0 do
  25.   begin                                     { loop 'til done    }
  26.     ch := Chr(48 + Byte(AnyLong) and $0F);  { 0..9 -> '0'..'9'  }
  27.     if ch > '9' then
  28.       Inc(ch, 7);                           { 10..15 -> 'A'..'F'}
  29.     HexString[Index] := ch;                 { insert Char       }
  30.     Dec(Index);                             { adjust chr Index  }
  31.     AnyLong := AnyLong SHR 4;               { For next nibble   }
  32.   end;
  33. end;
  34.  
  35. begin
  36.   ALong := $12345678;                       { a LongInt value   }
  37.   LongToHex(ALong, MyStr);                  { convert to hex str}
  38.   WriteLn;
  39.   WriteLn('$', MyStr);                      { display the String}
  40.   WriteLn;
  41. end.
  42.